package com.ascent.servlet;

import com.alibaba.fastjson.JSON;
import com.ascent.classes.User;
import com.ascent.service.UserServiceImgl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;


@WebServlet("/user/*")
public class UserServlet extends BaseServlet{

    private UserServiceImgl serviceImgl=new UserServiceImgl();

    /**
     * 用户登录
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String[] split = username.split(",");
        String a=split[0];
        String b=split[1];
        User user = serviceImgl.login(a,b);
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            //2.转为json
            String jsonString = JSON.toJSONString(user);
            //3.写数据
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
    }

    /**
     * 新用户注册
     */
    public void registerServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求体数据
        BufferedReader br = req.getReader();
        String params = br.readLine();
        //转为User对象
        User user = JSON.parseObject(params,User.class);
        user.setDel_flag(1);
        user.setSuperuser("1");
        System.out.println(user);
        User select = serviceImgl.selectByUser(user.getUsername());
        if(select == null){
            //2. 调用service添加
            serviceImgl.add(user);
            //3. 响应成功的标识
            resp.getWriter().write("success");
        }else{
            resp.getWriter().write("false");
        }
    }

//    public void selectFlag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String username = req.getParameter("username");
//        int flag = serviceImgl.selectFlag(username);
//
//        if(flag == 1){
//            HttpSession session = req.getSession();
//            session.setAttribute("del_flag",flag);
//            session.setAttribute("username",username);
//
//            resp.getWriter().write("success");
//        }else{
//            resp.getWriter().write("false");
//        }
//    }

    public void getSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        resp.setContentType("texy/json,charset=utf-8");
        String jsonString = JSON.toJSONString(user);
        resp.getWriter().write(jsonString);
    }

    /**
     * 查询所有用户
     */
    public void selectAllServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.调用service查询
        List<User> users = serviceImgl.selectAll();

        //2.转为json
        String jsonString = JSON.toJSONString(users);

        //3.写数据
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);

    }

    /**
     * 根据Uid修改用户信息
     */
    public void updateUserByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求体数据
        BufferedReader br = req.getReader();
        String params = br.readLine();
        //转为User对象
        User user = JSON.parseObject(params,User.class);
        serviceImgl.updateUser(user);
        resp.getWriter().write("success");
    }

    /**
     *根据uid查询用户信息
     */
    public void selectByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        User user = serviceImgl.selectByUid(Integer.parseInt(uid));
        //2.转为json
        String jsonString = JSON.toJSONString(user);
        //3.写数据
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);

    }
//    public void selectSuperByUserServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //接收username
//        String username = req.getParameter("username");
//        //调用service查询
//        User user = serviceImgl.selectSuperByUser(username);
//        // 存入Session
//        HttpSession session = req.getSession();
//        session.setAttribute("username",username);
//        //存储到request中
//        req.setAttribute("user",user);
//        //转发到changesuperuser.jsp中去
//        req.getRequestDispatcher("../product/changesuperuser.jsp").forward(req,resp);
//    }
//    public void updateSuperServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //处理post请求乱码
//        req.setCharacterEncoding("utf-8");
//        String superuser = req.getParameter("superuser");
//
//        HttpSession session = req.getSession();
//        Object username = session.getAttribute("username");
//
//        serviceImgl.updateSuper((String) username,superuser);
//        //转发到查询所有Servlet
//        req.getRequestDispatcher("/product/products_showusers.html").forward(req,resp);
//    }
//    public void updateFlag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //处理post请求乱码
//        req.setCharacterEncoding("utf-8");
//        String del_flag = req.getParameter("del_flag");
//        String username = req.getParameter("username");
//
//        if("1".equals(del_flag)){
//            del_flag="2";
//        }else{
//            del_flag="1";
//        }
//        serviceImgl.updateFlag(username,Integer.parseInt(del_flag));
//    }
}
